$(document).ready(function(){
       
        $("#seeIndex").on('click',function(){
            seeIndex()
        })
        
        seeIndex()
        function seeIndex(){
            $.ajax({
                url:'/indexUserSum',
                type:'get',
                success:function(res){
                    console.log(res.userNum)
                //     var html='';
                //     html+=`
                //         <p>${res.userNum}</p>
                //    `
                //     $('.userSum').html(html)

                    $.ajax({
                        url:'/indexarticleSum',
                        type:'get',
                        success:function(res1){
                            console.log(res1.articleNum)
                            var html='';
                            html+=`
                            <div class="Admin-index">
                                <h3>已注册用户的总数量</h3>
                                <div class="userSum">${res.userNum}</div>
                                <h3>发表的文章总数</h3>
                                <div class="articleSum">${res1.articleNum}</div>
                            </div>
                        `
                            $('.mainMiddle').html(html)
                        }
                    })
                }
            })     
            
            
        }
           
        
              
            //查询用户
        $('#seachUser').on('click',function(){
            console.log(111)
            $.ajax({
                url:'/searchUser',
                type:'get',
                success:function(res){
                   // console.log(res)
                    var html='';
                    html+=`
                    <div class="row mt">
                    <div class="col-md-12">
                        <div class="content-panel">
                            <table class="table table-striped table-advance table-hover">
                                    <h4><i class="fa fa-angle-right"></i>所有用户</h4>
                                    <hr>
                                <thead>
                                <tr>
                                    <th><i class="fa fa-bullhorn"></i> 用户ID</th>
                                    <th class="hidden-phone"><i class="fa fa-question-circle"></i> 用户名</th>
                                    <th><i class="fa fa-bookmark">注册时间</i> </th>
                                    <th><i class=" fa fa-edit"></i> Status</th>
                                    <th></th>
                                </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
  
          </section>
        </section>
            `
            $('.mainMiddle').html(html)
            var html1=''
            for(var i=0;i<res.seachUser.length;i++){
                html1+=`
                <tr class="${res.seachUser[i]._id}">
                <td><a href="javascript:;">${res.seachUser[i]._id}</a></td>
                <td class="hidden-phone">${res.seachUser[i].username}</td>
                <td>${res.seachUser[i].regDate}</td>
                <td><span class="label label-info label-mini">Due</span></td>
                <td>
                    <button class="btn btn-success btn-xs"><i class="fa fa-check"></i></button>
                    <button class="btn btn-primary btn-xs"><i class="fa fa-pencil"></i></button>
                    <button class="btn btn-danger btn-xs deleU"><i class="fa fa-trash-o "></i></button>
                </td>
            </tr>
                `
            }
                $('.mainMiddle .content-panel tbody').html(html1)
                //查询成功后删除用户
                $('.deleU').on('click',function(evt){
                    var id=$(evt.target).parent().parent().attr('class')
                    
                    $.ajax({
                        url:'/deleU',
                        type:'post',
                       data:{
                        id:id,
                       },
                        success:function(res){
                            console.log(res)
                           $(evt.target).parent().parent().remove()
                        },
                        error:function(res){
                            console.log(res)
                        }

                    })
                })


                },
                error:function(res){
                    console.log(res)
                }
                
            })
        })
        //查询文章
        $('#searchArcticle').on('click',function(){
            $.ajax({
                url:'/searchAtic',
                type:'get',
                success:function(res){
                   // console.log('1qqq')
                    console.log(res)
                    var html='';
                    html+=`
                    <div class="row mt">
                    <div class="col-md-12">
                        <div class="content-panel">
                            <table class="table table-striped table-advance table-hover">
                                    <h4><i class="fa fa-angle-right"></i>所有文章</h4>
                                    <hr>
                                <thead>
                                <tr>
                                    <th><i class="fa fa-bullhorn"></i>文章ID</th>
                                    <th class="hidden-phone"><i class="fa fa-question-circle"></i> 文章标题</th>
                                    <th><i class="fa fa-bookmark">文章发布时间</i> </th>
                                    <th><i class=" fa fa-edit"></i> Status</th>
                                    <th></th>
                                </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
  
          </section>
        </section>
            `
            $('.mainMiddle').html(html)
            var html1=''
            for(var i=0;i<res.Allarcticle.length;i++){
                html1+=`
                <tr class="${res.Allarcticle[i]._id}">
                <td><a href="javascript:;">${res.Allarcticle[i]._id}</a></td>
                <td class="hidden-phone">${res.Allarcticle[i].arctitle}</td>
                <td>${res.Allarcticle[i].pubtime}</td>
                <td><span class="label label-info label-mini">Due</span></td>
                <td>
                    <button class="btn btn-success btn-xs"><i class="fa fa-check"></i></button>
                    <button class="btn btn-primary btn-xs changeA"><i class="fa fa-pencil"></i></button>
                    <button class="btn btn-danger btn-xs deleA"><i class="fa fa-trash-o "></i></button>
                </td>
            </tr>
                `
            }
                $('.mainMiddle .content-panel tbody').html(html1)
                //查询文章成功后删除文章
                $('.deleA').on('click',function(evt){
                    var id=$(evt.target).parent().parent().attr('class')
                    
                    $.ajax({
                        url:'/deleArticle',
                        type:'post',
                       data:{
                        id:id,
                       },
                        success:function(res){
                            console.log(res)
                           $(evt.target).parent().parent().remove()
                        },
                        error:function(res){
                            console.log(res)
                        }

                    })
                })//删除文章结束
                //修改文章
                $('.changeA').on('click',function(evt){
                    
                    var id=$(evt.target).parent().parent().attr('class')
                    $('.mengban').css({
                        "display":"block"
                    })
                    $('.del').on('click',function(){
                        $('.mengban').css({
                            'display':'none'
                        })
                    })
                    //查询特定的文章
                    console.log(id)
                    $.ajax({
                        url:'/readArc',
                        type:'post',
                        data:{
                            id:id,
                        },
                        success:function(res){
                            console.log(res)
                            console.log(res.data[0].arctitle,res.data[0].content)
                            $('.uptitle').val(res.data[0].arctitle)
                             ue.setContent(res.data[0].content);
                             
                             //查询文章之后修改文章
                             $('.upBtn').on('click',function(){
                                var arctitle=$('.uptitle').val()
                                var content=ue.getContent()
                                console.log(content,arctitle)
                                $.ajax({
                                    url:'/uploadArc',
                                    type:'post',
                                    data:{
                                        id:id,
                                       arctitle:arctitle,
                                       content:content
                                    },
                                    success:function(res){
                                       console.log(res)
                                    }
                                })
                             })
                        },
                        error:function(res){
                            console.log(res)
                        }
                    })

                })


                },
                error:function(res){
                    console.log(res)
                }
            })
        })






    
})